package org.dromara.book.service.impl;

import org.dromara.book.domain.vo.SzlyBookInventoryVo;
import org.dromara.book.service.ISzlyBookInventoryService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBaseShelfBo;
import org.dromara.book.domain.vo.SzlyBaseShelfVo;
import org.dromara.book.domain.SzlyBaseShelf;
import org.dromara.book.mapper.SzlyBaseShelfMapper;
import org.dromara.book.service.ISzlyBaseShelfService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 层架管理Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class SzlyBaseShelfServiceImpl implements ISzlyBaseShelfService {

    private final SzlyBaseShelfMapper baseMapper;

    /**
     * 查询层架管理
     */
    @Override
    public SzlyBaseShelfVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询层架管理列表
     */
    @Override
    public List<SzlyBaseShelfVo> queryList(SzlyBaseShelfBo bo) {
        LambdaQueryWrapper<SzlyBaseShelf> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyBaseShelf> buildQueryWrapper(SzlyBaseShelfBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBaseShelf> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyBaseShelf::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SzlyBaseShelf::getCode, bo.getCode());
        lqw.eq(bo.getParentId() != null, SzlyBaseShelf::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 新增层架管理
     */
    @Override
    public Boolean insertByBo(SzlyBaseShelfBo bo) {
        SzlyBaseShelf add = MapstructUtils.convert(bo, SzlyBaseShelf.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改层架管理
     */
    @Override
    public Boolean updateByBo(SzlyBaseShelfBo bo) {
        SzlyBaseShelf update = MapstructUtils.convert(bo, SzlyBaseShelf.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBaseShelf entity){
        //TODO 做一些数据校验,如唯一约束
    }


    /**
     * 批量删除层架管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
